function checkLoginCookies() {
    if (CookieManager.getValue('email') !== null && CookieManager.getValue('email').length > 0) {
        login(CookieManager.getValue('email'), CookieManager.getValue('password'), false);
    }
}

function logout() {
    var start = new Date().getTime();
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        document.getElementById("info").innerHTML = ""; 
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            document.getElementById("login").innerHTML = xmlhttp.responseText;
            var end = new Date().getTime();
            var time = end - start;
            document.getElementById("info").innerHTML = "Logging out " + username + " took " + time/1000 + " seconds."; 
        }
    }
    xmlhttp.open("GET", "logout.php", true);
    xmlhttp.send();
}

function login(email, password, remember) {
    var start = new Date().getTime();
    console.log('email' + email);
    console.log('password' + password);
    console.log('remember' + remember);
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        document.getElementById("info").innerHTML = ""; 
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            document.getElementById("login").innerHTML = xmlhttp.responseText;
            var end = new Date().getTime();
            var time = end - start;
            document.getElementById("info").innerHTML = "Logging in " + email + " took " + time/1000 + " seconds."; 
        }
    }
    xmlhttp.open("GET", "login.php?email=" + email + "&password=" + password + "&remember=" + remember, true);
    xmlhttp.send();
}
function showResults(term, location, preference)
{
    var start = new Date().getTime();
    if (term.length == 0) 
    {                    
        document.getElementById("searchResults").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        document.getElementById("info").innerHTML = ""; 
        document.getElementById("animator").innerHTML = "<img src='./images/pacman.gif'>" + " chomping bits...";
        //this inner function just says 
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            if (preference === "dnk") {
                document.getElementById("animator").innerHTML = "You didn't care.";
            } else {
                document.getElementById("animator").innerHTML = "You wanted new places.";
            }
            document.getElementById("searchResults").innerHTML = xmlhttp.responseText;
            var end = new Date().getTime();
            var time = end - start;
            document.getElementById("info").innerHTML = "Searching Yelp, CityGrid and Facebook took " + time/1000 + " seconds."; 
        }
    }
    xmlhttp.open("GET", "combined_api_results.php?term=" + term + "&location=" + location, true);
    xmlhttp.send();
}

function showResultsJSON(term, location, preference)
{
    if (term.length == 0)
    {
        //clear out the                    
        document.getElementById("searchResults").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        document.getElementById("animator").innerHTML = "<img src='./images/pacman.gif'>" + " chomping bits...";
        //this inner function just says 
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            if (preference === "dnk") {
                document.getElementById("animator").innerHTML = "You didn't care.";
            } else {
                document.getElementById("animator").innerHTML = "You wanted new places.";
            }
            try {

                var places = JSON.parse(xmlhttp.responseText);
                var table = "<table class='results' align='center'>";
                table = table + ("<tr>");
//                            table = table + ("<td>Yelp URL</td><td>CityGrid URL</td><td>Rating</td><td>Yelp ID</td><td>CityGrid ID</td><td>Facebook Page ID</td>");
                for (var i = 0; i < places.length; i++) {
                    table = table + ("<td>");
                    table = table + ("This one is Yelp. </br>");
                    table = table + (places[i].name + "</br>");
                    table = table + ("</td>");
                }
                table = table + ("</tr>");
                table = table + ("</table>");
            } catch (err) {
                txt = "There was an error on this page.\n\n";
                txt += "Error description: " + err.message + "\n\n";
                txt += "Click OK to continue.\n\n";
                alert(txt);
            }
            document.getElementById("searchResults").innerHTML = table;
        }
    }
    xmlhttp.open("GET", "combined_api_results.php?term=" + term + "&location=" + location, true);
    xmlhttp.send();
}

function loadGeoIP()
{
    if (term.length == 0)
    {
        //clear out the 
        document.getElementById("location").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        //this inner function just says                    
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            document.getElementById("location").value = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "freegeoip.php", false);
    xmlhttp.send();
}

function beenThere(where)
{
    if (term.length == 0)
    {
        return;
    }
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        //this inner function just says                    
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            document.getElementById("info").innerHTML = xmlhttp.responseText;
            document.getElementById(where).setAttribute("style","visibility:hidden");
        }
    }
    xmlhttp.open("GET", "add_place.php?where=" + where, true);
    xmlhttp.send();
}

// CookieManager - Stephen Melvin 2012
// Sets/Gets cookie values
var CookieManager = (function() {

    return {
        // Sets a cookie where name = value;
        setValue: function(name, value) {
            document.cookie = name + "=" + encodeURIComponent(value);
        },

        // Retrieves the value of cookie 'name'.
        // Returns:
        // On Success getValue returns the value of the cookie requested,
        // otherwise getValue returns false.
        getValue: function(name) {
            var cookies = document.cookie;
            if (cookies === "") {
                return false;
            }

            var list = cookies.split("; ");
            var i;
            for (i = 0; i < list.length; i++) {
                var cookie = list[i];
                var p = cookie.indexOf("=");
                var cookiename = cookie.substring(0, p);
                var cookievalue = cookie.substring(p + 1);
                // Check if str1 is inside str2
                if (cookiename == cookiename.match(name)) {
                    return decodeURIComponent(cookievalue);
                }
            }

            return false;
        }
    };

})();
